package com.koron.css2.newProjectCenter.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.ConfigMapper;
import com.koron.css2.newProjectCenter.bean.PjCheckTemplateStaff;
import com.koron.css2.newProjectCenter.bean.query.PjCheckTemplateQuery;
import com.koron.css2.newProjectCenter.bean.query.PjCheckTemplateStaffQuery;
import com.koron.css2.newProjectCenter.mapper.PjCheckInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjCheckTemplateMapper;
import com.koron.css2.newProjectCenter.mapper.PjCheckTemplateStaffMapper;
import com.koron.css2.newProjectCenter.vo.PjCheckInfoVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

public class PjCheckInfoList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjCheckInfoMapper pjCheckInfoMapper = factory.getMapper(PjCheckInfoMapper.class);
            ConfigMapper configMapper = factory.getMapper(ConfigMapper.class);
            PjCheckTemplateStaffMapper pjCheckTemplateStaffMapper = factory.getMapper(PjCheckTemplateStaffMapper.class);
            PjCheckTemplateMapper pjCheckTemplateMapper = factory.getMapper(PjCheckTemplateMapper.class);
            Map<String, String> param = JsonUtils.objectToPojo(req.getData(), Map.class);
            List<PjCheckInfoVO> pjCheckInfoVOList = null;
            String CheckFlag = configMapper.getConfigValueByName("PJCHECK_TEMPLATE_FLAG");

            // 判断当前单号的当前部门的验收项是否存在；
            int iCount = pjCheckInfoMapper.countByBillNoAndDepartBelong(param.get("billNo"),param.get("departBelong"));
            // 1. 没有验证过
            if (iCount == 0) {
                // 1.1 通过用户账号获取对应的工程验收模板权限
                PjCheckTemplateStaffQuery pjCheckTemplateStaffQuery = new PjCheckTemplateStaffQuery();
                pjCheckTemplateStaffQuery.setCheckStaff(userInfo.getUserInfo().getAcount());
                PjCheckTemplateStaff pjCheckTemplateStaff = pjCheckTemplateStaffMapper.getSingle(pjCheckTemplateStaffQuery);
                // 1.2 根据权限查出具体的子项
                if (pjCheckTemplateStaff != null) {
                    PjCheckTemplateQuery pjCheckTemplateQuery = new PjCheckTemplateQuery();
                    pjCheckTemplateQuery.setDepartBelong(pjCheckTemplateStaff.getDepartBelong());
                    pjCheckInfoVOList = pjCheckTemplateMapper.getCheckInfoList(pjCheckTemplateQuery);
                }
                // 1.3 templateFlag
                if (pjCheckInfoVOList != null && pjCheckInfoVOList.size() > 0) {
                    pjCheckInfoVOList.stream().forEach(pjCheckInfoVO -> { pjCheckInfoVO.setTemplateFlag(CheckFlag); });
                }
            }// 2. 已经验证过
            else {
                int version = pjCheckInfoMapper.maxVersion(param.get("billNo"),param.get("departBelong"), userInfo.getUserInfo().getAcount());
                pjCheckInfoVOList = pjCheckInfoMapper.selectByBillNoAndVersionAndDepartBelong(param.get("billNo"), version,param.get("departBelong"));
                // 2.1 templateFlag
                if (pjCheckInfoVOList != null && pjCheckInfoVOList.size() > 0) {
                    pjCheckInfoVOList.stream().forEach(PjCheckInfoVO -> { PjCheckInfoVO.setTemplateFlag(CheckFlag); });
                }
            }
            MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "验收子项列表查询成功", List.class);
            info.setData(pjCheckInfoVOList);
            return info;
        } catch (Exception e) {
            logger.error("验收子项列表查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "验收子项列表查询失败", null);
        }
    }

}
